<?php

class Bootstrap {

    public $controler_path = "controllers/";
    public $model_path = "models/";
    public $view_path = "view/";

    function __construct() {
        if (isset($_GET["url"])) {
            $this->route();
        } else {
            $this->defaultRoute();
        }
    }

    function defaultRoute() {
//        require_once('../' . APP_PATH . $this->controler_path . "indexController.php");
        require_once(APP_PATH.$this->controler_path . "indexController.php");
        $index = new indexController();
        $index->index();
    }

    function route() {
        $url = explode("/", $_GET["url"]);
        $controller = $url[0];
        if (isset($url[1]))
            $action = $url[1];

        $filename = $controller . "Controller";
        if (is_file(APP_PATH . $this->controler_path . $filename . ".php")) {
            require_once(APP_PATH . $this->controler_path . $filename . ".php");
            $controller = new $filename();
            if (isset($action) && method_exists($controller, $action)) {
                $controller->$action($_REQUEST);
            } else {
                $controller->index($_REQUEST);
            }
        } else {
            require_once APP_PATH.$this->controler_path . "errorController.php";
            new errorController(array('controller'=>$filename,'action'=> isset($action) ? $action : ''));
        }
    }

}